package com.pointstar.dp.software;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 18年第二题：猜数字
 *
 * @author 朱科睿
 */
public class BullsAndCows {

    //要猜的那个数
    private final int secret;

    private static final Random random;

    /**
     * 有参数的构造方法，将secret设置为传入的参数
     *
     * @param secret 待猜测数字
     */
    public BullsAndCows(int secret) {
        this.secret = secret;
    }

    /**
     * 无参数的构造方法，随机生成四位数，且这个四位数没有重复数字，首位不能是0
     */
    public BullsAndCows() {
        String randomString = String.valueOf(generateFourDigitNumber());
        while (containsRepeatNumber(randomString)) {
            randomString = String.valueOf(generateFourDigitNumber());
        }
        secret = Integer.parseInt(randomString);
    }

    public int getSecret() {
        return secret;
    }

    static {
        random = new Random();
    }

    /**
     * 比较传入的guess与要猜的secret，返回几A几B
     *
     * @param guess 猜测的数字
     * @return 比较结果
     */
    public String compare(int guess) {
        int aNum = 0;
        int bNum = 0;
        char[] secretChars = String.valueOf(secret).toCharArray();
        char[] guessChars = String.valueOf(guess).toCharArray();
        for (int i = 0; i < secretChars.length; i++) {
            if (secretChars[i] == guessChars[i]) {
                aNum++;
            } else {
                for (char guessChar : guessChars) {
                    if (secretChars[i] == guessChar) {
                        bNum++;
                        break;
                    }
                }
            }
        }
        return String.format("%sA%sB", aNum, bNum);
    }

    /**
     * 模拟玩家的猜测过程，按要求输出
     */
    public void playGame() {
        int steps = 0;
        String delimiter = "----------";
        System.out.println("Game Start");
        System.out.println(delimiter);
        boolean endFlag = true;
        while (endFlag) {
            ++steps;
            if (steps > 10000) {
                System.out.println("More than 10000 steps. Game Over!");
                return;
            }
            int guess = generateFourDigitNumber();
            if (steps == 10000) {
                guess = secret;
            }
            System.out.printf("Guess:%s，Secret：%s%n", guess, secret);
            String compareResult = compare(guess);
            System.out.printf("Result:%s%n", compareResult);
            System.out.println(delimiter);
            if (compareResult.equals("4A0B")) {
                endFlag = false;
                System.out.printf("You win!You took %s steps.%n", steps - 1);
                System.out.print(delimiter);
            }
        }

    }

    public static void main(String[] args) {
        new BullsAndCows().playGame();
    }

    /**
     * 随机生成四位数
     *
     * @return 四位数
     */
    private int generateFourDigitNumber() {
        return random.nextInt(9000) + 1000;
    }

    /**
     * 是否包含重复的数字
     *
     * @param secret 待检验的数字字符串
     * @return 检验结果
     */
    private boolean containsRepeatNumber(String secret) {
        Set<Character> hashSet = new HashSet<>();
        char[] chars = secret.toCharArray();
        for (char c : chars) {
            hashSet.add(c);
        }
        return hashSet.size() < 4;
    }

}
